package edu.demo.recommend.controller;

import edu.demo.recommend.common.R;
import edu.demo.recommend.common.ResultCodeEnum;
import edu.demo.recommend.exception.RecommendBusinessException;
import edu.demo.recommend.pojo.Category;
import edu.demo.recommend.pojo.Shop;
import edu.demo.recommend.service.CategoryService;
import edu.demo.recommend.service.ShopService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author hb
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Resource
    private ShopService shopService;

    @Resource
    private CategoryService categoryService;

    @RequestMapping("/recommend")
    public R recommend(@RequestParam(name="longitude")  BigDecimal longitude, @RequestParam(name="latitude") BigDecimal latitude){
        if(Objects.isNull(longitude)||Objects.isNull(latitude)){
            throw new RecommendBusinessException(ResultCodeEnum.REQUEST_PARAM_ERROR);
        }

        List<Shop> shops = shopService.recommend(longitude, latitude);
        return R.ok().data("list",shops);
    }

    @RequestMapping("/search")
    public R search(@RequestParam(name="longitude")  BigDecimal longitude,
                    @RequestParam(name="latitude") BigDecimal latitude,
                    @RequestParam(name="keyword")String keyword,
                    @RequestParam(name="orderby",required = false)Integer orderby,
                    @RequestParam(name="categoryId",required = false)Integer categoryId,
                    @RequestParam(name="tags",required = false)String tags
                    ){
        if(Objects.isNull(longitude)||Objects.isNull(latitude)){
            throw new RecommendBusinessException(ResultCodeEnum.REQUEST_PARAM_ERROR);
        }

        List<Shop> shops = shopService.search(longitude, latitude,keyword,orderby,categoryId,tags);
        List<Category> categories = categoryService.selectAll();
        List<Map<String, Object>> tagsResult = shopService.searchGroupbyTags(keyword, categoryId, tags);
        return R.ok().data("list",shops).data("categoryList",categories).data("tags",tagsResult);
    }

}
